import EView as ev
import cv2 as cv
import time

blackRoute_factor = 1

def nothing(x):
    pass


if __name__ == '__main__':
    # 1. UART初始化
    # * 创建一个外设控制对象
    Peri = ev.Peripheral()
    # 设置波特率
    Peri.uartInit(115200)
    Peri.sendUartData("The serial port is initialized\r")

    # 2. 摄像头初始化
    Peri.sendUartData("The camera starts to initialize\r")
    # * 创建一个摄像头捕获对象
    Cap = cv.VideoCapture(0)
    if not Cap.isOpened():
        Peri.sendUartData("The Camera 0 failed to open\r")
        Cap = cv.VideoCapture(1)
    if not Cap.isOpened():
        Peri.sendUartData("The Camera 1 failed to open\r")
        Cap = cv.VideoCapture(2)
    if not Cap.isOpened():
        Peri.sendUartData("The Camera 2 failed to open\r")
        Cap = cv.VideoCapture(3)
    if not Cap.isOpened():
        Peri.sendUartData("The Camera 3 failed to open\r")
        Cap = cv.VideoCapture(4)
    if not Cap.isOpened():
        Peri.sendUartData("The Camera 4 failed to open\r")
        exit()

    # 摄像头读取测试
    ret, frame = Cap.read()
    # 如果正确读取帧，ret为True
    if not ret:
        Peri.sendUartData("The camera initialization failed\r")
        exit()
    Peri.sendUartData("The camera is initialized\r")

    # 3. 捕捉模块初始化
    Peri.sendUartData("The Capture module starts to initialize\r")
    # * 创建一个坐标捕捉对象,并设定初始模式为捕捉红色光斑
    PointCap = ev.Pointcapture()
    # 创建工作模式对象
    Work = ev.WorkModel()
    Work.model = 0
    Work.debug = True  # 开启debug模式

    # 4. 进入工作循环
    while True:
        # 工作模式发生变化发送新工作模式编码
        if Work.model != Work.oldmodel:
            Peri.sendUartData(f"STRNM%{Work .model}#RS\r")
        Work.oldmodel = Work.model  # 记录当前工作模式
        # 模式0：捕获红色光斑
        if Work.model == 0:
            tempbuf = Peri.getUartData(wait=False)
            Work.redCapture(Peri, PointCap, Cap)
            time.sleep(0.01)
            # 模式切换
            if tempbuf == "RSM1ST":
                cv.destroyAllWindows()
                Work.model = 1
            elif tempbuf == "RSM2ST":
                cv.destroyAllWindows()
                Work.model = 2
            
        # 模式1：计算黑框路线
        elif Work.model == 1:
            tempbuf = Peri.getUartData(wait=True)
            if tempbuf == "RSABRST":
                Work.blackRouteCapture(Peri, PointCap, Cap)
            # 模式切换
            elif tempbuf == "RSM0ST":
                cv.destroyAllWindows()
                Work.model = 0
            elif tempbuf == "RSM2ST":
                cv.destroyAllWindows()
                Work.model = 2
        # 模式3:参数调试
        elif Work.model == 2:
            tempbuf = Peri.getUartData(wait=True)
            if tempbuf == "RSS0ST":
                Work.setFov(PointCap, Cap)
            elif tempbuf == "RSS1ST":
                Work.setRedCaptureParam(PointCap, Cap)
            elif tempbuf == "RSS2ST":
                Work.setBlackRouteParam(PointCap,Cap)
            # 模式切换
            elif tempbuf == "RSM0ST":
                cv.destroyAllWindows()
                Work.model = 0
            elif tempbuf == "RSM1ST":
                cv.destroyAllWindows()
                Work.model = 1
